//
//  CYYHistoryViewController.m
//  LetsParty
//
//  Created by rimi on 15/9/21.
//  Copyright (c) 2015年 LetsPartyTeam. All rights reserved.
//

#import "CYYHistoryViewController.h"
#import "HFViewController.h"

static NSString * KUITableViewIdentifier = @"KUITableViewIdentifier";
//标题的大小
static NSInteger titleFontSize = 15;
//时间的字体大小
static NSInteger timeFontSize = 12;

@interface CYYHistoryViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView * mainTableView;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, strong) UIImageView * noDataImageView;//没有网络的时候的图片

- (void)initializeData;
- (void)initializeUserInterface;

@end

@implementation CYYHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.baseNavigationBar.backgroundColor = [UIColor whiteColor];
    self.bartitleLabel.text = @[@"所有历史活动",@"发起的历史活动",@"参加的历史活动",@"关注的历史活动"][self.index];
    [self.leftNavButton setImage:[UIImage imageNamed:@"iconfont-zuojiantou"] forState:UIControlStateNormal];
    [self.leftNavButton addTarget:self action:@selector(respondsToLeftButton:) forControlEvents:UIControlEventTouchUpInside];
    [self initializeUserInterface];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self initializeData];
}

#pragma mark - system protrol
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//section number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

//height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10.f;
}

//height
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.f;
}

//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //进入详情界面并传值
    HFViewController * hfVC = [[HFViewController alloc]init];
    hfVC.partyObjectId = ((BmobObject * )self.dataArray[indexPath.section]).objectId;
    hfVC.status = @"已过期";
    hfVC.laucher = [self.dataArray[indexPath.section] objectForKey:@"partySponsorInfo"][@"partySponsorName"];
    hfVC.partyTitleString = [self.dataArray[indexPath.section] objectForKey:@"partyTitle"];
    hfVC.partyDetailString = [self.dataArray[indexPath.section] objectForKey:@"partyDetailText"];
    hfVC.partyBeginTimeString = [self.dataArray[indexPath.section] objectForKey:@"partyBeginTime"];
    hfVC.partyEndTimeString = [self.dataArray[indexPath.section] objectForKey:@"partyEndTime"];
    hfVC.partyPositionNameString = [self.dataArray[indexPath.section] objectForKey:@"partyPositionName"];
    hfVC.partySecretCode = [self.dataArray[indexPath.section] objectForKey:@"partySecretCode"];
     __weak typeof(self) myVC = self;
    //获取参加人数
    [CYYNetWorkManager partyParticipatorsNumberWithPartyObjectId: ((BmobObject * )myVC.dataArray[indexPath.section]).objectId sucess:^(NSInteger partyParticipatorsNumber) {
        hfVC.partyParticipatorsNumber = [NSString stringWithFormat:@"%ld",(long)partyParticipatorsNumber];
        [myVC.navigationController pushViewController:hfVC animated:YES];
    } fail:^(NSError *error) {
        
    }];
}

//cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:KUITableViewIdentifier];
    cCell.textLabel.text = [NSString stringWithFormat:@"活动:%@\n",[self.dataArray[indexPath.section] objectForKey:@"partyTitle"]];
    cCell.textLabel.font = [UIFont boldSystemFontOfSize:titleFontSize];
    cCell.detailTextLabel.text = [NSString stringWithFormat:@"时间:%@ - %@",[self.dataArray[indexPath.section] objectForKey:@"partyBeginTime"],[self.dataArray[indexPath.section] objectForKey:@"partyEndTime"]];
    cCell.detailTextLabel.font = [UIFont systemFontOfSize:timeFontSize];
    cCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cCell;
}

#pragma mark - private methods
- (void)initializeData{
    switch (self.index) {
        case 0:{
            [CYYNetWorkManager getMyAllOutDateParties:^(NSArray *array) {
                _dataArray = [array mutableCopy];
                //判断有没有数据
                [self isNoDataImageViewByCount:_dataArray.count];
                [self.mainTableView reloadData];
            } fail:^(NSError *error) {
//                NSLog(@"%@",error);
                UIAlertController * alterController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"网络出错" preferredStyle:UIAlertControllerStyleAlert];
                [alterController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
                [self presentViewController:alterController animated:YES completion:nil];
            }];
        }
            break;
        case 1:{
            __weak typeof(self) myVC = self;
            [CYYNetWorkManager getMyAllOutDateSponsorPartiesSucessGet:^(NSArray *array) {
                _dataArray = [array mutableCopy];
                //判断有没有数据
                [myVC isNoDataImageViewByCount:_dataArray.count];
                [myVC.mainTableView reloadData];
            } fail:^(NSError *error) {
                
            }];
        }
            break;
        case 2:{
            __weak typeof(self) myVC = self;
            [CYYNetWorkManager getMyAllOutDateTakePartyInParties:^(NSArray *array) {
                _dataArray = [array mutableCopy];
                //判断有没有数据
                [myVC isNoDataImageViewByCount:_dataArray.count];
                [myVC.mainTableView reloadData];
            } fail:^(NSError *error) {
                
            }];
        }
            break;
        case 3:{
            __weak typeof(self) myVC = self;
            [CYYNetWorkManager getMyAllOutDateObserveParties:^(NSArray *array) {
                _dataArray = [array mutableCopy];
                //判断有没有数据
                [myVC isNoDataImageViewByCount:_dataArray.count];
                [myVC.mainTableView reloadData];
            } fail:^(NSError *error) {
                
            }];
        }
            break;
    }
}

- (void)initializeUserInterface{
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.mainTableView];
    [self.view addSubview:self.noDataImageView];
    self.noDataImageView.hidden = YES;
}

//隐藏\出现无数据图片
- (void)isNoDataImageViewByCount:(NSInteger)count{
    if (count == 0) {
        self.noDataImageView.hidden = NO;
    }else{
        self.noDataImageView.hidden = YES;
    }
}


#pragma mark - responds event
- (void)respondsToLeftButton:(UIButton *)button{
    [[ControllerManager sharedManager].rootViewController popViewControllerAnimated:YES];
}

#pragma mark - getter
- (UITableView *)mainTableView{
    if (!_mainTableView) {
        _mainTableView = ({
            UITableView * cTableView = [[UITableView alloc]initWithFrame:DHFlexibleFrame(CGRectMake(0, 64, 320, 504), NO) style:UITableViewStyleGrouped];
            cTableView.delegate = self;
            cTableView.dataSource = self;
            [cTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:KUITableViewIdentifier];
            cTableView;
        });
    }
    return _mainTableView;
}

- (UIImageView *)noDataImageView{
    if (!_noDataImageView) {
        _noDataImageView = ({
            UIImageView * cImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"mfq"]];
            cImageView.frame = DHFlexibleFrame(CGRectMake(0, 150, 320, 280), NO);
            cImageView;
        });
    }
    return _noDataImageView;
}

@end
